package com.wise.site.main;


import com.wise.site.R;
import com.wise.site.adapter.ArrayListAdapter;
import com.wise.site.adapter.BusinessAdapter;
import com.wise.site.adapter.BusinessAdapter.BUSINESS_TYPE;
import com.wise.site.adapter.InfoListAdatper;
import com.wise.site.adapter.InfoListAdatper.ITEM_TYPE;
import com.wise.site.base.BaseActivity;
import com.wise.site.detail.BuyDetailsActivity;
import com.wise.site.detail.InfoDetailsActivity;
import com.wise.site.detail.ShopsDetailsActivity;
import com.wise.site.detail.SupplyDetailsActivity;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.base.SoapItem;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.result.BussnissItem;
import com.wise.site.protocol.result.InfoItem;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.protocol.result.ShopItem;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.AlteredCharSequence;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ReferActivity extends BaseActivity implements OnClickListener{

	private ArrayListAdapter referinformation;
	private ListView mListView;
	public static final int INFO_TYPE = 0;
	public static final int SALE_TYPE = 1;
	public static final int BUY_TYPE = 2;
	public static final int SHOP_TYPE = 3;
	
	private TextView backtitle;
	private ImageButton back;
	private SoapAction<Integer> mAction = new SoapAction<Integer>(ACTION_TYPE.ACTION_COMMON, "delcollection"){

		@Override
		protected Integer parseJson(String response) throws Exception {
			return 1;
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_refer_member);
		backtitle =(TextView)findViewById(R.id.activity_title);
		back = (ImageButton)findViewById(R.id.title_back);
		back.setOnClickListener(this);
		
		LoginResult lr = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
		
		
		Intent intent = getIntent();
		final int type = intent.getIntExtra(Constanct.INTENT_COLLECTOR, -1);
		
		if(type == -1){
			finish();
			return;
		}
		
		mListView = (ListView) findViewById(R.id.listview);
		mListView.setLongClickable(true);
		mAction.addJsonParam("UserId", lr.id);
		
		switch (type) {
		case INFO_TYPE:{
			backtitle.setText(R.string.member_refer);
			final InfoListAdatper temp = new InfoListAdatper(ITEM_TYPE.INFO_TYPE, this, "showcollection");
			temp.setListView(mListView);
			temp.addJsonParam("CuserId", lr.id);
			temp.addJsonParam("Type", 3);
			mListView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					InfoItem item = (InfoItem) v.getTag();
					if(item != null){
						Intent intent = new Intent(getApplicationContext(), InfoDetailsActivity.class);
						intent.putExtra(Constanct.INFO_ENTRY, item);
						startActivity(intent);
					}
				}
			});
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					final InfoItem item = (InfoItem) v.getTag();
					showDialog("删除收藏", "是否删除咨询收藏", R.string.cancel, R.string.sure, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mAction.addJsonParam("Type", 3);
							mAction.addJsonParam("SubjectId", item.id);
							mAction.setActionListener(new ActionListener<Integer>() {
								@Override
								public void onSucceed(Integer result) {
									temp.loadData(false);
								}

								@Override
								public void onError(int resultCode) {
									
								}
							});
							ProtocolManager.getProtocolManager().submitAction(mAction);
						}
					});
					return false;
				}
			});
			temp.loadData(false);
			
			referinformation = temp;
			break;
		}
		case SALE_TYPE:{
			backtitle.setText(R.string.member_produce);
			final BusinessAdapter temp = new BusinessAdapter(BUSINESS_TYPE.SALE_TYPE, this, "showcollection");
			temp.setListView(mListView);
			temp.addJsonParam("CuserId", lr.id);
			temp.addJsonParam("Type", 1);
			temp.moveToFristPage();
			mListView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					BussnissItem item = (BussnissItem) v.getTag();
					if(item != null){
						Intent intent = new Intent(getApplicationContext(), SupplyDetailsActivity.class);
						intent.putExtra(Constanct.INFO_ENTRY, item);
						startActivity(intent);
					} 
				}
			});
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					final BussnissItem item = (BussnissItem) v.getTag();
					showDialog("删除收藏", "是否删除产品收藏", R.string.cancel, R.string.sure, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mAction.addJsonParam("Type", 1);
							mAction.addJsonParam("SubjectId", item.id);
							mAction.setActionListener(new ActionListener<Integer>() {
								@Override
								public void onSucceed(Integer result) {
									temp.loadData(false);
								}

								@Override
								public void onError(int resultCode) {
								}
							});
							ProtocolManager.getProtocolManager().submitAction(mAction);
						}
					});
					return false;
				}
			});
			temp.loadData(false);
			
			referinformation = temp;
			break;
		}
		case BUY_TYPE:{
			backtitle.setText(R.string.member_buy);
			final BusinessAdapter temp = new BusinessAdapter(BUSINESS_TYPE.BUY_TYPE, this, "showcollection");
			temp.setListView(mListView);
			temp.addJsonParam("CuserId", lr.id);
			temp.addJsonParam("Type", 2);
			temp.moveToFristPage();
			mListView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					BussnissItem item = (BussnissItem) v.getTag();
					if(item != null){
						Intent intent = new Intent(getApplicationContext(), BuyDetailsActivity.class);
						intent.putExtra(Constanct.INFO_ENTRY, item);
						startActivity(intent);
					} 
				}
			});
			
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					final BussnissItem item = (BussnissItem) v.getTag();
					showDialog("删除收藏", "是否删除求购收藏", R.string.cancel, R.string.sure, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mAction.addJsonParam("SubjectId", item.id);
							mAction.addJsonParam("Type", 2);
							mAction.setActionListener(new ActionListener<Integer>() {
								@Override
								public void onSucceed(Integer result) {
									temp.loadData(false);
								}

								@Override
								public void onError(int resultCode) {
								}
							});
							ProtocolManager.getProtocolManager().submitAction(mAction);
						}
					});
					return false;
				}
			});
			
			temp.loadData(false);
			
			referinformation = temp;
			
			break;
		}
		case SHOP_TYPE:{
			backtitle.setText(R.string.member_shop);
			
			final InfoListAdatper temp = new InfoListAdatper(ITEM_TYPE.SHOP_TYPE, this, "showcollection");
			temp.setListView(mListView);
			temp.addJsonParam("CuserId", lr.id);
			temp.addJsonParam("Type", 4);
			temp.moveToFristPage();
			mListView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					ShopItem item = (ShopItem) v.getTag();
					if(item != null){
						Intent intent = new Intent(getApplicationContext(), ShopsDetailsActivity.class);
						intent.putExtra(Constanct.INFO_ENTRY, item);
						startActivity(intent);
					} 
				}
			});
			
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View v,
						int arg2, long arg3) {
					final ShopItem item = (ShopItem) v.getTag();
					showDialog("删除收藏", "是否删除店铺收藏", R.string.cancel, R.string.sure, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mAction.addJsonParam("SubjectId", item.shopId);
							mAction.addJsonParam("Type", 4);
							mAction.setActionListener(new ActionListener<Integer>() {
								@Override
								public void onSucceed(Integer result) {
									temp.loadData(false);
								}

								@Override
								public void onError(int resultCode) {
								}
							});
							ProtocolManager.getProtocolManager().submitAction(mAction);
						}
					});
					return false;
				}
			});
			
			temp.loadData(false);
			
			referinformation = temp;
			break;
		}
			
			

		default:
			break;
		}
		
		

		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.title_back:
			finish();
			break;
		}
	}
	
}